<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_user_feedback extends user_feedback {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 	取出一条信息
     */

    public function do_detail($uf_id = '') {
        global $_G;
        $detail = array();
        $detail = $this->one_info('user_feedback', 'uf_id', $uf_id);
        //$course_info = self::get_field_value_by_id('course_main', 'cm_id', $detail['cm_id'], 'cm_id,cm_name,cm_brief,cm_img,cm_price,cm_count,so_id,so_id,cm_hour_num');
        unset($detail['create_dateline']);
        unset($detail['modify_dateline']);
        unset($detail['uf_isfeedback']);
        unset($detail['us_user_id']);
        unset($detail['uf_root_id']);
        unset($detail['uf_parent_id']);
        //DEBUG 获取课程信息
        //DEBUG 调试输出
        if ($_G['gp_api']) {
            $return['code'] = '0';
            $return['data'][$_G['gp_mod']][$_G['gp_action']][$_G['gp_do']] = $detail;
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=user&action=cart&do=index&keyword=" . $keyword);
            include template('trade/course/detail');
        }
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        /*
            $user_feedback = array(
                'user_id'=>$_G['user_id'],//DEBUG 反馈用户
                'uf_content'=>'',//DEBUG 问题描述
                'uf_file'=>'',//DEBUG 反馈文件
                'uf_email'=>'',//DEBUG 您的邮箱
                'uf_phone'=>'',//DEBUG 您的手机号码
                'create_dateline'=>TIMESTAMP
            );
        */
        $user_feedback = isset($_REQUEST['user_feedback']) && !empty($_REQUEST['user_feedback']) ? $_REQUEST['user_feedback'] : '';
        if ($_G['gp_api']) {
            //TODO 后端字段校验 根据具体业务逻辑添加
            if(!empty($user_feedback)){
                $user_feedback['user_id'] = $_G['user_id'];
                $user_feedback['create_dateline'] = TIMESTAMP;
                $insert_id = $this->add('user_feedback', $user_feedback);
            }
            $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
            //DEBUG 获取课程信息
            if ($_G['gp_api']) {
                if ($insert_id && empty($cm_id_noexist)) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
                if ($client == 'pc') {
                    $return['data'] = array();
                } else {
                    $return['data'][$_G['gp_mod']][$_G['gp_action']][$_G['gp_do']] = array();
                }
                echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
            }else{
                if ($insert_id) {
                    showmessage('操作成功', 'index.php?mod=user&action=cart&do=index');
                } else {
                    showmessage('操作失败', 'index.php?mod=user&action=cart&do=index');
                } 
            }
        } else {
            //include template('trade/course/add');
        }
    }
    /*
     *  编辑系统信息
     */

    public function do_edit_todo() {
        global $_G;

    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        $uf_id = isset($_REQUEST['uf_id']) && !empty($_REQUEST['uf_id']) ? $_REQUEST['uf_id'] : '';
        if(!empty($user_feedback['uf_id'])){
            $wheresql = " uf_id IN (".  dimplode($uf_id).") ";
            $effect_row = $this->delete("user_feedback", $wheresql, $limit = 100);
        }
        $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        //DEBUG 获取课程信息
        if ($_G['gp_api']) {
            if ($effect_row) {
                $return['code'] = '0';
            } else {
                $return['code'] = '1';
            }
            if ($client == 'pc') {
                $return['data'] = array();
            } else {
                $return['data'][$_G['gp_mod']][$_G['gp_action']][$_G['gp_do']] = array();
            }
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }else{
            //TODO 
        }
    }

    /*
     * 列表TODO
     */

    public function do_index_todo() {
        global $_G;
        $default_limit = 1000;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : $default_limit;
        $start = (($page - 1) * $perpage);
        $wheresql = " AND user_id='" . $_G['user_id'] . "' AND isdelete=0 ";
        $orderby = " ORDER BY create_dateline DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('user_feedback', $page_condition);
        $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        //DEBUG 获取课程信息
        if ($_G['gp_api']) {
            $return['code'] = '0';
            if ($client == 'pc') {
                $return['data'] = $page_result['page_data'];
            } else {
                $return['data'][$_G['gp_mod']][$_G['gp_action']][$_G['gp_do']] = $page_result;
            }
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            //TODO
        }
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY uf_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('user_feedback', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>